package com.yc.jpyy.ciitems.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.ciitems.service.CiitemsService;
import com.yc.jpyy.utils.Tools;

@Controller
@RequestMapping(value = "/ciitems")

public class CiitemsController extends BaseController {
	
	@Autowired
	private CiitemsService ciitemsService;
    private static Logger logger = LoggerFactory.getLogger(CiitemsController.class);
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/ciitemdetails", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String ddAdd(HttpServletRequest request) throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		String info = request.getParameter("info");
		Map<String,Object> paramsMap = Tools.JsonToMap(info);
		String dbcode = paramsMap.get("dbcode").toString();
		try{
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>>  val = ciitemsService.ciitemsService(paramsMap);
			if(val.size()==0) {
				response.setReturnCode(0);
				response.setReturnMsg("无查询数据");
				return response.toJSON();
			}
			else {
				response.setReturnMsg(1, "操作成功");
				response.setListData(val);
				return response.toJSON();
			}
			
		} catch (Exception e) {
			logger.info("数据字典异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			e.printStackTrace();
		}
		
		finally {
			return response.toJSON();
		}
		
	}
	
	/**
	 * 获取二保的二级参数
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTwoInsuranceCitems", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTwoInsuranceCitems(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = ciitemsService.getTwoInsuranceCitems(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getTwoInsuranceCitems" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "获取失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	
}
